/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			pglnpattern.cc
  \date			Jun 2012
  \author		TNick

  \brief		Contains the implementation of PgLnPattern class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<cpg/cpg.h>

#include	<QStringList>
#include	<cpg/logic/cpgdoc.h>
#include	<cpg/logic/layer/pglayglob.h>
#include	<cpg/logic/layer/pglayspc.h>
#include	<cpg/logic/pgctx.h>

#include	"pglnpattern.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace cpg;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
PgLnPattern::PgLnPattern	( CpgDoc * doc )
	: PgCharacteristic( doc )
{
	qreal		space = 4;
	dashes_ << 1 << space << 3 << space << 9 << space
			<< 27 << space << 9 << space;
	off_ = 0;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgLnPattern::~PgLnPattern	( void )
{
	/* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgLnPattern *			PgLnPattern::clone				( CpgDoc * destin, int & idx )
{
	Q_ASSERT( destin != NULL );
	PgLnPattern * ret = new PgLnPattern( destin );

	ret->dashes_ = dashes_;
	ret->off_ = off_;
	ret->setName( name() );

	destin->addLnPatt( ret );
	idx = destin->lnPattCount() - 1;

	return ret;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				PgLnPattern::nameAndId		(
		QStringList & name_list, QList<int> & id_list )
{

#	define	nameAndId_helper(i)	\
	id_list.append( i ); name_list.append( nameForId( i ) );

	nameAndId_helper( BYLAYER );
	nameAndId_helper( BYBLOCK );
	nameAndId_helper( NOPEN );
	nameAndId_helper( SOLID );
	nameAndId_helper( DASH );
	nameAndId_helper( DOT );
	nameAndId_helper( DASHDOT );
	nameAndId_helper( DASHDOTDOT );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgLnPattern::isIdValid		( CpgDoc * doc, int id )
{
	if ( id >= 0 )
	{
		if ( id < doc->lnPattCount() )
			return true;
		return false;
	}
	else
	{
		switch		( id )	{
		case	NOPEN:
		case	SOLID:
		case	DASH:
		case	DOT:
		case	DASHDOT:
		case	DASHDOTDOT:
		case	BYLAYER:
		case	BYBLOCK:
			return true;
		default:
			return false;
		}
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString				PgLnPattern::asString			( CpgDoc * doc, int id )
{
	if ( id >= 0 )
	{
		if ( id < doc->lnPattCount() )
			return doc->lnPatt( id )->name();
		return QString();
	}
	else
	{
		return nameForId( (Id)id );
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString				PgLnPattern::nameForId		( Id id )
{
	switch		( id )	{
	case	NOPEN:
		return QObject::tr( "no pen" );
	case	SOLID:
		return QObject::tr( "solid" );
	case	DASH:
		return QObject::tr( "dash" );
	case	DOT:
		return QObject::tr( "dot" );
	case	DASHDOT:
		return QObject::tr( "dash-dot" );
	case	DASHDOTDOT:
		return QObject::tr( "dash-dot-dot" );
	case	BYLAYER:
		return QObject::tr( "by layer" );
	case	BYBLOCK:
		return QObject::tr( "by block" );
	default:
		return QString();
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QVector<qreal>		PgLnPattern::explicitPattern		(
		PgCtx * ctx, int id )
{
	QVector<qreal>		ret;

	for ( ;; )
	{
		switch		( id )	{
		case	NOPEN:
			ret << 0 << 1;
			break;
		case	SOLID:
			ret << 1 << 0;
			break;
		case	DASH:
			ret << 4 << 1;
			break;
		case	DOT:
			ret << 1 << 1;
			break;
		case	DASHDOT:
			ret << 4 << 1 << 1 << 1;
			break;
		case	DASHDOTDOT:
			ret << 4 << 1 << 1 << 1 << 1 << 1;
			break;
		case	BYLAYER:
			if ( ctx->layer_ == NULL )
			{
				Q_ASSERT( false );
				id = SOLID;
			}
			else
			{
				id = ctx->layer_->lnPattern();
				if ( id == BYLAYER )
				{
					Q_ASSERT( false );
					id = SOLID;
				}
			}
			continue;
		case	BYBLOCK:
			if ( ctx->block_def_ == NULL )
			{
				Q_ASSERT( false );
				id = SOLID;
			}
			else
			{
				/** @todo use block's own method, as it may be part of another block
				id = ctx->block_def_->lnPattern();
				ctx->block_def_ = ctx->block_def_->block(); */
				id = SOLID;
				Q_ASSERT( false );
			}
			continue;
		default:
			if ( id < ctx->document_->lnPattCount() )
			{
				ret = ctx->document_->lnPatt( id )->pattern();
			}
			else
			{
				Q_ASSERT( false );
				id = SOLID;
				continue;
			}
		}

		break;
	}

	return ret;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				PgLnPattern::asXML					(
		PgCtx * ctx, PgLnPattern::Id id, QDomElement & dest )
{
/*
	QDomDocument x_doc = dest.ownerDocument();
	QDomComment cmnt = x_doc.createComment(
				"pg-pattern="+asString( ctx->document_, id ) );
	dest.appendChild( cmnt );
	dest.setAttribute( "pg-pattern", id );

	QVector<qreal> lst = PgLnPattern::explicitPattern( ctx, id );
	QString s;
	foreach( qreal val, lst )
	{
		s.append( QString::number( val ) );
		s.append( "|" );
	}
	dest.setAttribute( "pg-pattern-explicit", s );
*/
	/** @todo asXML */
	Q_UNUSED( ctx );
	Q_UNUSED( id );
	Q_UNUSED( dest );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
int					PgLnPattern::copy			(
		CpgDoc * source, CpgDoc * destin, int value )
{
	Q_ASSERT( source != NULL );
	Q_ASSERT( destin != NULL );
	Q_ASSERT( isIdValid( source, value ) );

	/* can we directly use this value? */
	if ( source == destin )
		return value;

	if ( value >= 0 )
	{
		int i;
		/*PgLnPattern new_col = */ source->lnPatt( value )->clone( destin, i );
		return i;
	}
	else
	{
		return value;
	}
}
/* ========================================================================= */

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
